package web

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/util/gconv"
	"strconv"
)

//Login 登录
func Login(r *ghttp.Request) {
	_ = r.Response.WriteTpl("admin/login/login.html")
}

//Test 链铺导出区域和行业
func Test(r *ghttp.Request) {
	res, err := g.DB().Table("lp_area").All()
	if err != nil {
		glog.Error(err)
		r.ExitAll()
	}
	f := excelize.NewFile()
	// Create a new sheet.
	index := f.NewSheet("区域")
	f.SetActiveSheet(index)
	field := []string{"id", "pid", "name", "full_name", "pinyin", "path_id", "path_name", "path_full_name"}
	head := []string{"ID", "上级ID", "简称", "全称", "拼音", "路径ID", "路径简称", "路径全称"}
	f.SetSheetRow("区域", "A1", &head)
	for k, obj := range res.List() {
		//行数据处理
		row := make([]string, 0)
		for _, vv := range field {
			row = append(row, gconv.String(obj[vv]))
		}
		//添加行
		lint := strconv.Itoa(k + 2)
		f.SetSheetRow("区域", "A"+lint, &row)
	}
	//行业
	res2, _ := g.DB().Table("lp_shop_category").All()
	h := f.NewSheet("行业")
	f.SetActiveSheet(h)
	field2 := []string{"id", "pid", "name", "path_id", "path_name"}
	head2 := []string{"ID", "上级ID", "名称", "路径ID", "路径名称"}
	f.SetSheetRow("行业", "A1", &head2)
	for k, obj := range res2.List() {
		//行数据处理
		row := make([]string, 0)
		for _, vv := range field2 {
			row = append(row, gconv.String(obj[vv]))
		}
		//添加行
		lint := strconv.Itoa(k + 2)
		f.SetSheetRow("行业", "A"+lint, &row)
	}
	//end
	if err := f.SaveAs("log/area.xlsx"); err != nil {
		fmt.Println(err)
	}
	r.Response.ServeFileDownload("log/area.xlsx")
}
